package com.sscaipiao.android.activity.ShiShiCai;

import android.os.Bundle;
import android.view.View;
import com.sscaipiao.android.R;
import com.sscaipiao.android.activity.Base.NormalActivity;
import com.sscaipiao.android.utils.ShakeListener;
import com.sscaipiao.android.utils.StringUtil;
import com.sscaipiao.android.widget.BallBase;
import com.sscaipiao.android.widget.RedBall;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Airay.Rong on 14-1-2.
 */
public class SSYiXingZhiXuanActivity extends NormalActivity implements BallBase.onClickCallback {
	private int BALL_NUM_IN_GROUP = 10;

	private List<Integer> BallIds = new ArrayList<Integer>();

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addContainerMain(R.layout.ss_yi_xing_zhi_xuan_activity);
		setupUI(true, false, true, true, true);
		setTitleText("一星直选");
		setInfo("从0~9中选一个至多个号码投注，投注号码与当期开奖号码的个位号码相符，即为中奖");
		addShakeListener();
		addBalls();
	}
	private void addBalls() {
		BallIds.add(R.id.red_ball_0);
		BallIds.add(R.id.red_ball_1);
		BallIds.add(R.id.red_ball_2);
		BallIds.add(R.id.red_ball_3);
		BallIds.add(R.id.red_ball_4);
		BallIds.add(R.id.red_ball_5);
		BallIds.add(R.id.red_ball_6);
		BallIds.add(R.id.red_ball_7);
		BallIds.add(R.id.red_ball_8);
		BallIds.add(R.id.red_ball_9);
		for (int i = 0; i < BALL_NUM_IN_GROUP; i++) {
			getRedBallByIndex(i).setCallback(this);
		}
	}
	private BallBase getRedBallByIndex(int index) {
		return  (RedBall)findViewById(BallIds.get(index));
	}
	private void addShakeListener() {
		ShakeListener shakeListener = new ShakeListener(this);
		shakeListener.setOnShakeListener(new ShakeListener.OnShakeListener() {
			public void onShake() {
				randomChooseBalls();
			}

		});
	}
	private void randomChooseBalls() {
		resetAllBalls();
		Integer[] randomRedBalls = StringUtil.getRandom(10, 1);
		for (int num : randomRedBalls) {
			RedBall Ball = (RedBall) (findViewById(BallIds.get(num-1)));
			Ball.setSelected(true);
		}

		reCalculatorSum();
	}
	private void resetAllBalls() {
		for (int i = 0; i <BALL_NUM_IN_GROUP; i++) {
			RedBall ball = (RedBall) findViewById(BallIds.get(i));
			ball.reset();
		}
	}
	private void reCalculatorSum() {
		String sAgeFormat1 = getResources().getString(R.string.double_color_ball_hint);
		int notecount = notecount();
		setMoney(String.format(sAgeFormat1, notecount + "", (notecount * 2) + ""));
	}
	private int notecount() {
		int notenum = 1;
//		for (int i = 0; i < 6; i++) {
//			notenum = notenum * (queryChoosedRedBallNum() - i);
//		}
//		notenum = notenum / 720;
//		notenum = notenum * queryChoosedBlueBallNum();
		return notenum;
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (R.id.btn_radom == v.getId()) {
			randomChooseBalls();
		} else if (R.id.im_undo == v.getId()) {
			resetAllBalls();
			reCalculatorSum();
		} else if (R.id.btn_confirm == v.getId()) {
			//doConfirm();
		}
	}

	@Override
	public void onBallClick(View me) {

	}
}